﻿using System;
using System.Collections;
using System.Collections.Generic;
using CXedu.Model;
using Ajeelee.Core.Database;
using Ajeelee.Core.Utils;

namespace CXedu.Business
{
    public interface INewsClassBusinessFactory
    {
        INewsClassBusiness CreateNewsClassBusiness();
    }
    public class NewsClassBusinessFactory : BusinessFactory, INewsClassBusinessFactory
    {
        public INewsClassBusiness CreateNewsClassBusiness()
        {
            return new Default.NewsClassBusiness();
        }
    }
    ///<summary>
    /// interface
    ///</summary>
    public interface INewsClassBusiness : IBusinessBase<NewsClass, int>
    {
        ///<summary>
        ///根据ID值读取NewsClass实体
        ///</summary>
        NewsClass GetById(int id);
        ///<summary>
        ///根据NewsClass实体插入数据库
        ///</summary>
        object Insert(NewsClass entity);
        ///<summary>
        ///根据NewsClass实体更新数据库
        ///</summary>
        int Update(NewsClass entity);
        ///<summary>
        ///<summary>
        ///根据HashTable更新数据库(只更新指定字段)
        ///</summary>
        int UpdateSomeVal(Hashtable table);
        ///<summary>
        ///根据NewsClass实体删除数据库
        ///</summary>
        int Delete(NewsClass entity);
        ///<summary>
        ///根据NewsClass实体ID删除数据库
        ///</summary>
        int Delete(int id);
        ///<summary>
        ///根据NewsClass实体ID删除数据库
        ///</summary>
        int DeleteByPid(int id);
        /// <summary>
        /// 读取所有记录
        /// </summary>
        IList<NewsClass> GetAllNewsClass();
        /// <summary>
        /// 根据objQuery条件读取所有记录，objQuery可用值类型、hashtable、类实例
        /// </summary>
        IList<NewsClass> GetAllNewsClass(object objQuery);
        /// <summary>
        /// 根据objQuery查询条件和page分页条件读取相应分页记录(先查所有再用List手动处理分页)
        /// </summary>
        IList<NewsClass> GetAllNewsClass(object objQuery, PageStructure page);
        /// <summary>
        /// 根据table查询条件和page分页条件读取相应分页记录(每次只查需要数据)
        /// </summary>
        IList<NewsClass> GetAllNewsClassByPage(Hashtable table, PageStructure page);
        /// <summary>
        /// 根据objQuery条件读取条数，objQuery可用值类型、hashtable、类实例
        /// </summary>
        int GetAllCount(Hashtable table);

    }
}
namespace CXedu.Business.Default
{
    ///<summary>
    ///
    ///</summary>
    public class NewsClassBusiness : BusinessBase<NewsClass, int>, INewsClassBusiness
    {

        ///<summary>
        ///根据ID值读取NewsClass实体
        ///</summary>
        public NewsClass GetById(int id)
        {
            if (id == null)
                throw new BusinessException("Invalid_Id_Value");
            NewsClass entity = GetById("NewsClass.NewsClass_GetById", id);
            return entity;
        }
        ///<summary>
        ///根据NewsClass实体插入数据库
        ///</summary>
        public object Insert(NewsClass entity)
        {
            if (entity.Id == null)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Insert("NewsClass.NewsClass_Insert", entity);
        }
        ///<summary>
        ///根据NewsClass实体更新数据库
        ///</summary>
        public int Update(NewsClass entity)
        {
            if (entity.Id == null)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Update("NewsClass.NewsClass_Update", entity);
        }
        ///<summary>
        ///根据Hashtable实体更新数据库(只更新指定字段)
        ///</summary>
        public int UpdateSomeVal(Hashtable entity)
        {
            return Update("NewsClass.NewsClass_UpdateSomeVal", entity);
        }
        ///<summary>
        ///根据NewsClass实体删除数据库
        ///</summary>
        public int Delete(NewsClass entity)
        {
            if (entity.Id == null)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Delete("NewsClass.NewsClass_Delete", entity);
        }
        ///<summary>
        ///根据NewsClass实体ID删除数据库
        ///</summary>
        public int Delete(int id)
        {
            if (id <= 0)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            NewsClass entity = new NewsClass { Id = id };
            entity.Id = id;
            return Delete("NewsClass.NewsClass_Delete", entity);
        }
        ///<summary>
        ///根据NewsClass实体ID删除数据库
        ///</summary>
        public int DeleteByPid(int pid)
        {
            if (pid <= 0)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            NewsClass entity = new NewsClass { PId = pid };
            entity.PId = pid;
            return Delete("NewsClass.NewsClass_DeleteByPid", entity);
        }
        /// <summary>
        /// 读取所有记录
        /// </summary>
        public IList<NewsClass> GetAllNewsClass()
        {
            return GetAllNewsClass(null);
        }
        /// <summary>
        /// 根据objQuery条件读取所有记录，objQuery可用值类型、hashtable、类实例
        /// </summary>
        public IList<NewsClass> GetAllNewsClass(object objQuery)
        {
            return GetAllNewsClass(objQuery, null);
        }
        /// <summary>
        /// 根据objQuery查询条件和page分页条件读取相应分页记录
        /// </summary>
        public IList<NewsClass> GetAllNewsClass(object objQuery, PageStructure page)
        {
            return QueryForList("NewsClass.NewsClass_GetAll", objQuery, page);
        }
        /// <summary>
        /// 根据table查询条件和page分页条件读取相应分页记录(每次只查需要数据)
        /// </summary>
        public IList<NewsClass> GetAllNewsClassByPage(Hashtable table, PageStructure page)
        {
            if (page.CurrPage == 0 || page.PageSize == 0)
            {
                return new List<NewsClass>();
            }

            page.Counts = GetAllCount(table);
            page.PageCount = (page.Counts % page.PageSize == 0) ? page.Counts / page.PageSize : page.Counts / page.PageSize + 1;

            if (page.CurrPage == 1)
            {
                table.Add("TopNum", page.PageSize);
                return GetAllNewsClass(table);
            }

            int max = (page.CurrPage - 1) * page.PageSize;
            if (max < 500)
            {
                table.Add("maxsize", max);
                table.Add("pagesize", page.PageSize);
                return QueryForList("NewsClass.NewsClass_GetByPageNotIn", table, null);
            }
            else
            {
                table.Add("pagesize", page.Counts - max > page.PageSize ? page.PageSize : page.Counts - max);
                table.Add("currcount", page.CurrPage * page.PageSize);
                if (table.ContainsKey("OrderDirection"))
                {
                    table["OrderDirection"] = table["OrderDirection"].ToString().ToUpper();
                }
                return QueryForList("NewsClass.NewsClass_GetByPage3Top", table, null);
            }
        }
        /// <summary>
        /// 根据objQuery条件读取条数，objQuery可用值类型、hashtable、类实例
        /// </summary>
        public int GetAllCount(Hashtable table)
        {
            return Convert.ToInt32(QueryForScalar("NewsClass.NewsClass_GetAllCount", table));
        }
    }
}
